package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.domian.*;
import com.itheima.domian.vo.QueryVo2;
import com.itheima.domian.vo.SetmealVo;
import com.itheima.mapper.admin.SetmealMapper;
import com.itheima.service.admin.SetmealDishService;
import com.itheima.service.admin.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 12441
 *
 * @Date 2022/8/11 15:13
 * @Version 1.0
 */
@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealMapper queryMapper;
    @Transactional
    @Override
    public void addSetmeal(SetmealVo setmealVo) {
            this.save(setmealVo);

            //加工：取出dishVo的flavor属性添加id
            //Stream流map方法可以将字符串转换为对象接收，使代码的灵活性更高
            Long setmealVoId = setmealVo.getId();

            log.info("============"+setmealVoId);

            List<SetmealDish> setmealDishes = setmealVo.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((item)-> {
                item.setSetmealId(setmealVoId);
                return item;
            }).collect(Collectors.toList());

            log.info(setmealDishes.toString());

            setmealDishService.saveBatch(setmealDishes);

        }
    @Override
    public IPage<QueryVo2> findByPage(Page<QueryVo2> page, QueryWrapper<QueryVo2> queryWrapper) {
        return queryMapper.findByPage(page, queryWrapper);
    }
    @Override
    @Transactional
    public void updateWithDish(SetmealVo setmealVo) {
        this.updateById(setmealVo);

        //
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealVo.getId());
        setmealDishService.remove(queryWrapper);

        //添加新的口味信息
        List<SetmealDish> setmealDishes = setmealVo.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((item)-> {
            item.setSetmealId(setmealVo.getId());
            return item;
        }).collect(Collectors.toList());


        setmealDishService.saveBatch(setmealDishes);
    }
    @Override
    public SetmealVo getByIdWithDish(Long id) {
        //查询菜品
        Setmeal setmeal=this.getById(id);

        SetmealVo setmealVo = new SetmealVo();

        BeanUtils.copyProperties(setmeal,setmealVo);

        //查询口味数组，赋值
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDishService.list(queryWrapper);
        setmealVo.setSetmealDishes(setmealDishes);
        return setmealVo;
    }

    @Override
    public void removeSetmealWithDish(List<Long> ids) {
        //判断状态是否为停售
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.in(Setmeal::getId,ids);
        lambdaQueryWrapper.eq(Setmeal::getStatus,1);
        int count = this.count(lambdaQueryWrapper);
        if(count>0){
            throw new CustomException("套餐正在售卖，不能删除，请先停售！");
        }
        this.removeByIds(ids);

        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SetmealDish::getSetmealId);
        setmealDishService.remove(queryWrapper);
    }

    @Override
    public void transStatus(List<Long> ids, Integer status) {
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.in(Setmeal::getId,ids);
        List<Setmeal> setmeals = queryMapper.selectList(lambdaQueryWrapper);
        log.info(setmeals.toString());
        for (Setmeal setmeal : setmeals) {
            setmeal.setStatus(status);
            this.updateById(setmeal);
        }
    }

}
